import { Context } from 'koa';
import categoryService from '../services/category.service';

/**
 * 获取分类列表
 * @param ctx Koa上下文
 */
export const getCategories = async (ctx: Context) => {
  try {
    const { 
      parent_id, 
      keyword, 
      include_article_count, 
      sort_by, 
      sort_order,
      status
    } = ctx.query;

    const params = {
      parent_id: parent_id as string,
      keyword: keyword as string,
      include_article_count: include_article_count === 'true',
      sort_by: sort_by as string,
      sort_order: sort_order as 'ASC' | 'DESC',
      status: status as 'active' | 'inactive',
    };

    const result = await categoryService.getCategories(params);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '获取分类列表成功',
    };
  } catch (error: any) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: `获取分类列表失败: ${error.message}`,
    };
  }
};

/**
 * 获取分类详情
 * @param ctx Koa上下文
 */
export const getCategoryById = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    
    const category = await categoryService.getCategoryById(id);
    
    ctx.body = {
      code: 200,
      data: category,
      message: '获取分类详情成功',
    };
  } catch (error: any) {
    ctx.status = 404;
    ctx.body = {
      code: 404,
      message: `获取分类详情失败: ${error.message}`,
    };
  }
};

/**
 * 创建分类
 * @param ctx Koa上下文
 */
export const createCategory = async (ctx: Context) => {
  try {
    const {
      name,
      slug,
      description,
      parent_id,
      order,
      status,
    } = ctx.request.body;
    
    // 验证必填字段
    if (!name || !slug) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '分类名称和别名不能为空',
      };
      return;
    }
    
    const category = await categoryService.createCategory({
      name,
      slug,
      description,
      parent_id,
      order,
      status,
    });
    
    ctx.status = 201;
    ctx.body = {
      code: 201,
      data: category,
      message: '创建分类成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `创建分类失败: ${error.message}`,
    };
  }
};

/**
 * 更新分类
 * @param ctx Koa上下文
 */
export const updateCategory = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    const {
      name,
      slug,
      description,
      parent_id,
      order,
      status,
    } = ctx.request.body;
    
    const category = await categoryService.updateCategory(id, {
      name,
      slug,
      description,
      parent_id,
      order,
      status,
    });
    
    ctx.body = {
      code: 200,
      data: category,
      message: '更新分类成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `更新分类失败: ${error.message}`,
    };
  }
};

/**
 * 删除分类
 * @param ctx Koa上下文
 */
export const deleteCategory = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    const { transfer_to } = ctx.query;
    
    const result = await categoryService.deleteCategory(id, transfer_to as string);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '删除分类成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `删除分类失败: ${error.message}`,
    };
  }
};

/**
 * 更新分类排序
 * @param ctx Koa上下文
 */
export const updateCategoriesOrder = async (ctx: Context) => {
  try {
    const { order_data } = ctx.request.body;
    
    if (!Array.isArray(order_data) || order_data.length === 0) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '排序数据格式不正确',
      };
      return;
    }
    
    const result = await categoryService.updateCategoriesOrder(order_data);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '更新分类排序成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `更新分类排序失败: ${error.message}`,
    };
  }
}; 